<div class="layui-fluid">
	<div class="layui-row">
		<div class="layui-col-xs12">
			<div class="layui-card layui-backbox">
				<div class="layui-card-header">新增名师交流</div>
				<div class="layui-card-body" style="display: flex;">
					<form class="layui-form" action="/" lay-filter="richfilter">
						<!-- 			<input type="hidden" name="tbId">
 -->
						<div class="layui-form-item">
							<label class="layui-form-label">课程类型</label>
							<div class="layui-input-inline">
								<select name="type" id="coursetype" lay-filter="coursetypefilter" lay-search="">
									<option value="1">视频课程</option>
									<option value="2">线下公益</option>
									<option value="3">实操课程</option>
								</select>
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">选择课程</label>
							<div class="layui-input-inline">
								<select name="tbId">
								</select>
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">问题详情</label>
							<div class="layui-input-inline">
								<textarea id="demo" name="content1" style="display: none;"></textarea>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">讲师回答</label>
							<div class="layui-input-inline">
								<textarea id="demo2" name="content2" style="display: none;"></textarea>
							</div>
						</div>

						<div class="layui-form-item">
							<div class="layui-input-block">

								<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
								<div class="layui-btn layui-btn-primary" id="returnqx">取消</div>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- 脚本 -->
<script>
	layui.use(['form', 'layedit'], function() {
				var form = layui.form,
					$ = layui.jquery,
					layedit = layui.layedit;
				form.render();
				var richdemo = layedit.build('demo');
				var richdemo2 = layedit.build('demo2');
				var router = layui.router();
				POSTAJAX(VideoGet, {
					currentPage: 1,
					pageSize: 100
				}, function(res) {
					$.each(res.data.data, function(idx, obj) {

						$("select[name='coursetbId']").append('	<option value="' + obj.id + '">' + obj.title + '</option>');
					})
					form.render();
				})
				POSTAJAX(VideoGet, {
					currentPage: 1,
					pageSize: 100
				}, function(res) {
					$.each(res.data.data, function(idx, obj) {

						$("select[name='tbId']").append('	<option value="' + obj.id + '">' + obj.title + '</option>');
					})
					form.render();
				})


				form.on('select(coursetypefilter)', function(data) {
$("select[name='tbId']").empty()
					switch (data.value) {
						case '1':
							//视频课程
							POSTAJAX(VideoGet, {
								currentPage: 1,
								pageSize: 100
							}, function(res) {
								$.each(res.data.data, function(idx, obj) {
							
									$("select[name='tbId']").append('	<option value="' + obj.id + '">' + obj.title + '</option>');
								})
								form.render();
							})
									break;
									case '2':
									//线下公益	
									POSTAJAX(XXGet, {
										currentPage: 1,
										pageSize: 100,
										type:1
									}, function(res) {
										$.each(res.data.data, function(idx, obj) {
											$("select[name='tbId']").append('	<option value="' + obj.tbId + '">' + obj.tbTitle + '</option>');
										})
										form.render();
									})
									break;
									case '3':
													//实操课程
									POSTAJAX(XXGet, {
										currentPage: 1,
										pageSize: 100,
										type:2
									}, function(res) {
										$.each(res.data.data, function(idx, obj) {
									
											$("select[name='tbId']").append('	<option value="' + obj.tbId + '">' + obj.tbTitle + '</option>');
										})
										form.render();
									})
					
								
									break;
									default:
									break;
								}


							})



					if (router.search.tbId) {
						// 有id的传参的值  查询 id
						POSTAJAX(MsjlById, {
							tbId: router.search.tbId
						}, function(res) {
							form.val("richfilter", {
								"content1": res.data.tbProblemContent , //详情
								"content2": res.data.tbAnswerContent , //	回答
								"tbId":router.search.tbId, 
								"type": res.data.type
							})
							layedit.setContent(richdemo, res.data.tbProblemContent );
						layedit.setContent(richdemo2, res.data.tbAnswerContent );
						})

						//修改
						form.on('submit(formDemo)', function(data) {
							if (layedit.getContent(richdemo) == '') {
								layer.msg("请输入详情内容", {
									"icon": 7
								})
							} else {
								data.field.tbAgreementContent = layedit.getContent(richdemo)
								data.field.tbId = router.search.tbId;
								POSTAJAX(AgreementMod, data.field, function(res) {
									if (res.status == 200) {
										layer.msg("修改成功", {
											"icon": 1
										})
										richdemo = layedit.build('demo');
										form.render();
										location.href = '#/setup/list'
									} else {
										layer.msg("失败，请重试", {
											"icon": 2
										})
									}
								})
							}
							return false;
						});

					} else {
						// 新增
						//监听提交
						form.on('submit(formDemo)', function(data) {
							if (layedit.getContent(richdemo) == '' || layedit.getContent(richdemo2) == '') {
								layer.msg("请输入详情内容", {
									"icon": 7
								})
							} else {
								data.field.content1 = layedit.getContent(richdemo)
								data.field.content2 = layedit.getContent(richdemo2)
								// data.field.tbId = router.search.videotbId;
								console.log(data.field)
								POSTAJAX(MsjlAdd, data.field, function(res) {
									if (res.status == 200) {
										layer.msg("新增成功", {
											"icon": 1
										})
										richdemo = layedit.build('demo');
										form.render();
										location.href = '#/teachersexchange/list'
									} else {
										layer.msg("失败，请重试", {
											"icon": 2
										})
									}
								})
							}
							return false;
						});

					}
					$("#returnqx").on("click", function() {
						location.href = '#/setup/list';

					})


				});
</script>
<!-- 样式 -->
<style scoped>
	.layui-form-item .layui-input-inline {
		float: left;
		width: 600px;
		margin-right: 10px;
	}
</style>
